package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class ChangeUserMessage extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView tv_right;
    private String titles;

    private TextView tv_name;
    private EditText et_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_user_message);

        initData();
        initView();
    }

    private void initData() {
        titles = getIntent().getStringExtra("title");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(titles);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("保存");
        tv_right.setTextColor(0xff008c99);

        tv_name = (TextView) findViewById(R.id.tv_name);
        et_content = (EditText) findViewById(R.id.et_content);
        et_content.setText(getIntent().getStringExtra("str") + "");
        tv_name.setText(titles);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //保存
            case R.id.submit:
                if ("行业".equals(titles)) {
                    upload1();
                } else if ("职业".equals(titles)) {
                    upload2();
                } else if ("公司名称".equals(titles)) {
                    upload3();
                }
                break;

            default:
                break;
        }
    }

    private String content;
    private static final String TAG = "ChangeUserMessage";

    private void upload3() {
        showDialog();
        content = et_content.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("company", content);
        XUtil.Post(Url.MEMBER_SETMEMBERINFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: sex= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
//                    if ("-1".equals(res)) {
//                        //info = "请重新登录";
//                        MyToast("请重新登录");
//                        startActivity(new Intent(ChangeUserMessage.this, LoginNewActivity.class));
//                        return;
//                    }
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        MyToast(info);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void upload2() {
        showDialog();
        content = et_content.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("position", content);
        XUtil.Post(Url.MEMBER_SETMEMBERINFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: sex= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        MyToast(info);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void upload1() {
        showDialog();
        content = et_content.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("profession", content);
        XUtil.Post(Url.MEMBER_SETMEMBERINFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: sex= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        MyToast(info);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }
}
